import pygame, random

class star:
	def __init__(self, x, y, img):
		self.x = x
		self.y = y
		self.img = img
	
	def get_surface(self):
		return self.img
		
class star_layer:
	def __init__(self, star_num, speed, direction, star_sample, res):
		self.stars = []
		self.direction = direction
		self.speed = speed
		for i in range(0,star_num):
			x = random.randint(0, res[0])
			y = random.randint(0, res[1])
			chosen_star_sample = star_sample[random.randint(0,len(star_sample)-1)]
			self.stars.append(star(x, y, chosen_star_sample ))
		self.star_num = star_num
		self.res = res
		
	def move(self):
		if self.direction == "down":
			for i in range(0,self.star_num):
				self.stars[i].y = self.stars[i].y + self.speed
				if self.stars[i].y > self.res[1]:
					self.stars[i].y = 0
					self.stars[i].x = random.randint(0, self.res[0])
		elif self.direction == "up":
			for i in range(0,self.star_num):
				self.stars[i].y = self.stars[i].y - self.speed
				if self.stars[i].y < 0:
					self.stars[i].y = self.res[1]
					self.stars[i].x = random.randint(0, self.res[0])
		elif self.direction == "left":
			for i in range(0,self.star_num):
				self.stars[i].x = self.stars[i].x - self.speed
				if self.stars[i].x < 0:
					self.stars[i].x = self.res[0]
					self.stars[i].y = random.randint(0,self.res[1])
		elif self.direction == "right":
			for i in range(0,self.star_num):
				self.stars[i].x = self.stars[i].x + speed
				if self.stars[i].x > self.res[0]:
					self.stars[i].x = 0
					self.stars[i].y = random.randint(0, self.res[1])
	
class starfield:
	def __init__(self):
		self.star_layers = []
		
	def add_layer(self,star_num, speed, direction, star_sample, res):
		simgs = []
		for samp in star_sample:
			col = (samp[0],samp[1], samp[2])
			size = samp[3]
			star = pygame.Surface((size*2,size*2))
			pygame.draw.circle(star,col,(size,size),size)
			simgs.append(star)
		self.star_layers.append(star_layer(star_num, speed, direction, simgs, res))
			
	def draw(self, bg_surf):
		for slayer in self.star_layers:
			for strs in slayer.stars:
				bg_surf.blit(strs.get_surface(), (strs.x, strs.y))
			slayer.move()
	
	
				
				